package ${BasePackageName}${ControllerPackageName};

import ${EntityPackageName}.${EntityName};
import ${EntityPackageName}.${EntityName}List;
import ${EntityPackageName}.${EntityName}Search;
import ${BasePackageName}${ServicePackageName}.${EntityName}Service;
import WebApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
import WebResponseDto;
import DefaultBusinessContext;
import java.util.Date;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.BeanUtils;

/**
 * @author: ${Author}
 * @date: ${DateTime}
 * @description:
 */
@WebApi
@RequestMapping("/web/${ClassName}Controller")
@RestController
public class ${ClassName}Controller {
    @Autowired
    private ${EntityName}Service ${EntityNameForCamelCase}ServiceImpl;

   @RequestMapping(value = "/listPage",method = RequestMethod.POST)
   public WebResponseDto listPage(@RequestBody ${EntityName}Search search){
       WebResponseDto responseDto = new WebResponseDto();

       List<${EntityName}List> list =  ${EntityNameForCamelCase}ServiceImpl.selectByPageNumSize(search,search.getPageDto().getPageNum(),search.getPageDto().getPageSize()) == null ? null : (List<${EntityName}List>)${EntityNameForCamelCase}ServiceImpl.selectByPageNumSize(search,search.getPageDto().getPageNum(),search.getPageDto().getPageSize());
       if(CollectionUtil.isNotEmpty(list)){
           PageInfo page = new PageInfo(list);
           PageDto pageDto = new PageDto();
           BeanUtils.copyProperties(page,pageDto);
           responseDto.setPage(pageDto);
           responseDto.setLstDto(list);
       }
       return responseDto;
   }



   @RequestMapping(value = "",method = RequestMethod.POST)
   public WebResponseDto create(@RequestBody  ${EntityName} entity){
       WebResponseDto responseDto = new WebResponseDto();
       DefaultBusinessContext ctx = DefaultBusinessContext.getContext();
       entity.setCreaterId(ctx.getAccountId());
       entity.setCreaterName(ctx.getAccountName());
       entity.setCreateTime(new Date());
       ${EntityNameForCamelCase}ServiceImpl.insert(entity);
       return responseDto;
   }


   @RequestMapping(value = "",method = RequestMethod.PUT)
   public WebResponseDto update(@RequestBody  ${EntityName} entity){
       WebResponseDto responseDto = new WebResponseDto();
       DefaultBusinessContext ctx = DefaultBusinessContext.getContext();
       entity.setModifyId(ctx.getAccountId());
       entity.setModifyName(ctx.getAccountName());
       entity.setModifyTime(new Date());
       ${EntityNameForCamelCase}ServiceImpl.updateByPrimaryKeySelective(entity);
       return responseDto;
   }


   @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
   public WebResponseDto delete(@PathVariable Long id){
       WebResponseDto responseDto = new WebResponseDto();
       ${EntityNameForCamelCase}ServiceImpl.deleteByPrimaryKey(id);
       return responseDto;
   }



    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public WebResponseDto detail(@PathVariable Long id){
        WebResponseDto responseDto = new WebResponseDto();
        ${EntityName} entity = ${EntityNameForCamelCase}ServiceImpl.selectByPrimaryKey(id);
        if(entity != null){
            responseDto.setObj(entity);
        }else{
            responseDto.setFlag(WebResponseDto.INFO_WARNING);
            responseDto.setMessage("没有该记录！");
            return responseDto;
        }
        return responseDto;
    }
}
